<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH.'libraries/facebook/facebook.php';
class Gallery extends CI_Controller {
  var $user_id;
  public function __construct(){
    parent::__construct();
    // $this->output->enable_profiler(TRUE);
    global $facebook;
    $this->user_id = $facebook->getUser();

    $this->load->model('Users_model');
  }

	public function index($page = 1, $order = 'default', $open_avatar = '')
	{
    $avatars = $this->Users_model->get_avatars(AVATARS_PER_PAGE, ($page-1) * AVATARS_PER_PAGE);
    $total_avatars = $this->Users_model->total_avatars();

    $this->load->view('header');
    $this->load->view('gallery', array('avatars' => $avatars->result_array(), 'pager_html' => _theme_pager('gallery/index', $page, $total_avatars, AVATARS_PER_PAGE), 'first_parts' => first_part_sentences(), 'open_avatar' => $open_avatar));
    $this->load->view('footer');
  }

  function single($aid) {
    $avatar = $this->Users_model->get_avatar_by_id($aid);
    $this->load->model('Votes_model');
    $voted = 0;
    if($this->user_id && ($this->Votes_model->get_entry(array('uid' => $aid, 'voter_id' => $this->user_id)) || $avatar->user_id == $this->user_id)) {
      $voted = 1;
    }
    $this->load->view('gallery_single', array('avatar' => $avatar, 'first_parts' => first_part_sentences(), 'voted' => $voted));
  }
  /*
  public function vote($aid) {
    $this->load->model('Votes_model');
    if($this->input->is_ajax_request() && $this->user_id && $this->Users_model->get_entry(array('id' => $aid, 'status' => 1)) && !$this->Votes_model->get_entry(array('uid' => $aid, 'voter_id' => $this->user_id))) {
      if($this->Votes_model->insert_entry($aid, $this->user_id)) {
        echo json_encode(array('message' => 'success', 'votes' => $this->Votes_model->get_avatar_votes($aid)));
      }
      else {
        echo 'error';
      }
    }
    exit;
  }
  */
}